﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Model;
using System.IO;
using AnyOffice.DAL;
using AnyOffice.Components;
using AnyOffice.Appcode;


namespace AnyOffice.Web.general.system.netdiskGX
{
    public partial class NDiskTypeForm : BasePage
    {
        public readonly DiskTypeGXDao ty = new DiskTypeGXDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["DiskTypeID"]))
                {
                    hdd_diskid.Value = Request.QueryString["DiskTypeID"].ToString();
                    Model.DiskTypeGX dty = ty.GetModel(base.Conn, Convert.ToInt32(hdd_diskid.Value));
                    BindInit(dty);
                }
            }
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //检测此计算机上的盘符
            string[] arr = Directory.GetLogicalDrives(); 
            string aa=txt_NDiskPath.Text.Substring(0,txt_NDiskPath.Text.IndexOf("\\"))+"\\";
            if (!arr.Contains(aa.ToUpper())) 
            {
                MessageBox.Show(this, " 无效盘符，请确认盘符是否存在!");
                return;
            }
            int num = 0;
            if (hdd_diskid.Value.Length > 0)
            {
                num = UpdateDiskType();
            }
            else
            { 
                num = AddDiskType();
            }
            if (num > 0)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "parent.menu_main.location.href='NDiskType.aspx';");
            }
            else { MessageBox.Show(this, "保存失败，请查看目录路径是否已存在！"); } 

        }

        /// <summary>
        /// 添加操作
        /// </summary>
        /// <returns></returns>
        public int AddDiskType()
        {
            if (ty.IsExitName(base.Conn, txt_NDiskName.Text.Trim()))
            {
                return 0;
            }
            Model.DiskTypeGX dt = new Model.DiskTypeGX();//实体对象
            dt = AddBind(dt);//添加赋值
            if (CreateFolder(txt_NDiskPath.Text.Trim())) //判断目录路径是否存在
            {
                return ty.Add(base.Conn, dt);
            }
            return 0;

        }

        /// <summary>
        /// 更新操作
        /// </summary>
        /// <returns></returns>
        public int UpdateDiskType() 
        {
            Model.DiskTypeGX dt = ty.GetModel(base.Conn, Convert.ToInt32(hdd_diskid.Value));
            if (dt.DiskTypeName != txt_NDiskName.Text.Trim())//编辑时，如果名称修改了，判断是否已存在此名称
            {
                if (ty.IsExitName(base.Conn, txt_NDiskName.Text.Trim()))
                {
                    return 0;//如果存在返回0
                }
            }
            //物理路径有修改
            if (dt.BasePath != txt_NDiskPath.Text.Trim())
            {
                //如果是相同盘符做路径修改
                if (Path.GetPathRoot(dt.BasePath) == Path.GetPathRoot(txt_NDiskPath.Text.Trim()))
                {
                    //判断修改物理文件夹名是否成功
                    if (Public_foldGX.RemoveFiles(dt.BasePath, txt_NDiskPath.Text.Trim(), "1"))
                    {
                        dt = AddBind(dt);
                        ty.Update(base.Conn, dt); //修改数据库记录
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
                else //不同盘符数据库修改，物理路径不做修改
                {
                    dt = AddBind(dt);
                    ty.Update(base.Conn, dt);
                    return 1;
                }
            }
            if (dt.DiskTypeName != txt_NDiskName.Text.Trim()) //如果名称修改了
            {
                dt = AddBind(dt);//修改数据库记录
                ty.Update(base.Conn, dt);
                return 1;
            }
            return 1;
        }


        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="dis"></param>
        public void BindInit(Model.DiskTypeGX dis)  
        {
            txt_NDiskName.Text = dis.DiskTypeName;
            if (dis.DiskTypeName == Public_foldGX.DiskTypeName || dis.DiskTypeName == Public_foldGX.DiskTypeName2)
            {
                txt_NDiskName.Enabled = false;
            }
            txt_NDiskPath.Text = dis.BasePath;
        }


        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="dis"></param>
        public Model.DiskTypeGX AddBind(Model.DiskTypeGX dis)
        {
            if (txt_NDiskName.Text.Trim() != "")
            { dis.DiskTypeName = txt_NDiskName.Text.Trim(); }
            if (txt_NDiskPath.Text.Trim() != "")
            { dis.BasePath = txt_NDiskPath.Text.Trim(); }
           
            return dis;
        }



        /// <summary>
        /// 创建物理文件夹
        /// </summary>
        /// <param name="FolderPathName"></param>
        protected bool CreateFolder(string FolderPathName)
        {
            if (FolderPathName.Trim().Length > 0)
            {
                try
                {
                    if (!Directory.Exists(FolderPathName))
                    {
                        Directory.CreateDirectory(FolderPathName);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    return false;
                }

            }
            return false;
        }
    }
}